package com.sdk.paas.core.utils;

import com.alibaba.fastjson.JSONObject;
import com.sdk.paas.core.HttpLogEntity;
import com.sdk.paas.core.enums.ErrorEnum;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author limaosheng
 * @create 2023-02-02 9:10
 * @desc
 */
public class LogUtil {
    /**
     * 请求超时或者其它异常
     *
     * @param httpLogEntity
     * @param e
     * @author limaosheng
     * @create 9:13 2023/2/2
     */
    public static String logExc(HttpLogEntity httpLogEntity, Exception e) {
        httpLogEntity.setErrorMsg(ErrorEnum.REQUEST_ERROR.getDesc());
        httpLogEntity.setException(e);
        return comm(httpLogEntity);
    }

    /**
     * 请求状态码非200
     *
     * @param httpLogEntity
     * @author limaosheng
     * @create 9:15 2023/2/2
     */
    public static String httpCodeErr(HttpLogEntity httpLogEntity) {
        httpLogEntity.setErrorMsg(ErrorEnum.REQUEST_FAIL_ERROR.getDesc());
        return comm(httpLogEntity);
    }

    /**
     * 请求成功
     *
     * @param httpLogEntity
     * @author limaosheng
     * @create 9:16 2023/2/2
     */
    public static String comm(HttpLogEntity httpLogEntity) {
        Long endTime = System.currentTimeMillis();
        httpLogEntity.setEndTime(endTime);
        httpLogEntity.setReqTime(endTime - httpLogEntity.getStartTime());
        httpLogEntity.setPrintTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(new Date()));
        return JSONObject.toJSONString(httpLogEntity);
    }

}
